<?
	include("common.php");

	if(!$loggedin){
		ob_start();
		header("Location: login.php");
	}
	include("pop_header.php");
	$qr1 = mysql_query("SELECT * FROM forum WHERE id=$forum");
	$i = "1";
	$forums = mysql_fetch_object($qr1);
?>
<p><font face=arial size=3>
<b>Start New Thread in <?=$forums->name?></b><br>
<table border="0" cellpadding="10" cellspacing="0" width="85%" align="center">
<tr>
	<td align="left">
<?
	if( $step2 ) {
		$mb = $forum;
		$id = $topic;
		$select_ck_board = mysql_query("SELECT * FROM forum WHERE id='$forum'");
		$select_ck_thread = mysql_query("SELECT * FROM thread WHERE id='$topic'");
		if( empty($title) or empty($body) ) {
			$if_error = "1";
			$input = "1";
		} else if(mysql_num_rows($select_ck_board) == 0) {
			$if_error = "1";
			$input = "2";
		} else if(mysql_num_rows($select_ck_thread) == 0) {
			$if_error = "1";
			$input = "3";
		}
		// ERROR HANDLING
		if($if_error == "1") {
			if($input == "1") {
				$message = "<b>A required field was left blank.</b><br><br>Please use the back button on your browser, or <a href=\"javascript:history.back(1)\">click here</a>, to return to the previous form. You must fill in the \"<b>Subject</b>\" and \"<b>Body</b>\" fields in order to properly post a reply.";
			} 
			if($input == "2") {
				$message = "<b>Forum does not exist.</b><br><br>The forum you have attempted to post to does not exist in our system. Please return to the <a href=\"$siteurl/index.php\">forum home</a> to select a valid forum.";
			} 
			if($input == "3") {
				$message = "<b>Thread does not exist.</b><br><br>The thread you have attempted to reply to does not exist in our system. Please return to the <a href=\"$siteurl/index.php\">forum home</a> to select a valid forum and thread.";
			}
			include("errormsg.php");
		} else {
			// SET TIME & DATE
			$date = time()+$timeadjust;

			// INSERT REPLY
			mysql_query ("INSERT INTO posts SET
									threadid='$topic', 
									uid='$myuid',
									title='$title',
									body='$body',
									timestamp='$date'") or die( mysql_error() );
?>
			<table border="0" cellpadding="20" cellspacing="0" width="100%" height="100%">
			<tr>
				<td width="100%">
					<table border="0" cellpadding="8" cellspacing="0" width="100%" bgcolor="#F5F5F5">
					<tr>
						<td width="100%"><meta http-equiv="refresh" content="2; url=<?=$siteurl?>/topic.php?topic=<?=$topic?>&forum=<?=$forum?>"><div align="center"><font face="verdana, arial, helvetica, sans-serif" size="2"><b>Your post is being processed... You will be redirected to the thread display shortly.</b></font></div></td>
					</tr>
					</table>
				</td>
			</tr>
			</table>
<?
		}
	} else {
		$select_subject = "SELECT title FROM thread WHERE id='$topic'";
		$data_subject = mysql_query($select_subject);
		$var_subject = mysql_fetch_array($data_subject);
		if(empty($var_subject[title])) {
			$display_title = "Untitled Thread";
		} else {
			$display_title = $var_subject[title];
		}
?>
		<table border="0" cellpadding="20" cellspacing="0" width="100%">
		<tr>
			<td width="100%" valign="top">
				<div align="justify"><font face="arial, verdana, helvetica, sans-serif" size="2"><br><br>To post a new thread on the <?=$sitename?> forum please fill out the following form below. All fields are required to be properly filled out with information. Please be aware that HTML tags are not allowed.</font></div>
				<form method="post">
				<input type="hidden" name="topic" value="<?=$topic?>">
				<input type="hidden" name="forum" value="<?=$forum?>">
				<table border="0" cellpadding="1" cellspacing="0" width="100%">
				<tr>
					<td width="100%" colspan="2">
						<table border="0" cellpadding="8" cellspacing="1" width="100%" height="34" style='border: 1px solid #000000;background-color:#e5e5e5;'>
						<tr>
							<td>
								<font face="verdana, arial, helvetica, sans-serif" size="2" color="#000000"><b>Replying to '<?=$display_title?>'</b></font>
							</td>
						</tr>
						</table>
					</td>
				 </tr>
				<tr>
					<td width="100%">
						<table border="0" cellpadding="8" cellspacing="1" width="100%">
						<tr bgcolor="#F5F5F5">
							<td NOWRAP><font face="verdana, arial, helvetica, sans-serif" size="2"><b>Subject:</b></font></td>
							<td width="100%"><input type="text" name="title" size="40" value="Re: <?=$display_title?>"></td>
						</tr>
						<tr bgcolor="#FAFAFA">
							<td valign="top" NOWRAP><font face="verdana, arial, helvetica, sans-serif" size="2"><b>Body:</b><font size="1"><br>(50,000 maximum characters)</font></font></td>
							<td width="100%"><textarea name="body" cols="63" rows="15" tabindex="2"></textarea></td>
						</tr>
						<tr bgcolor="#F5F5F5">
							<td width="100%" align="right" colspan="2"><input type="submit" name="step2" value="Submit Thread"></td>
						</tr>
						</table>
					</td>
				</tr>
				</table>
				</form>
			</td>
		</tr>
		</table>
<?
	}
?>
	</td>
</tr>
</table>
</font></p>
<?
	include("pop_footer.php");
?>